package TestVolatile;

/**
 * @author tongchen
 * @create 2023-06-15 23:57
 */
public class WaitAndNotify {
    public static Object locker1=new Object();
    public static Object locker2=new Object();
    public static void main(String[] args) throws InterruptedException {
        //创建线程1
         Thread thread1 =new Thread(()->{
             System.out.println("线程1开始执行......");
             synchronized (locker1){
                 try {
                     System.out.println("线程1暂停，等待线程2执行......");
                     locker1.wait();
                     System.out.println("线程1继续向下执行");
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 }
             }
             System.out.println("线程1执行结束");
                 });
        //创建线程2
         Thread thread2 =new Thread(()->{
             System.out.println("线程2开始执行");
             synchronized (locker2){
                 locker1.notify();
                 System.out.println("线程2准备资源完毕，交由线程1处理");
             }
             System.out.println("线程2执行结束.......");
                 });
         thread1.start();
         thread2.start();
         thread1.join();
         thread2.join();
    }
}
